﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ScrollMemories : MonoBehaviour
{

    List<UnitStatus> GraveList = new List<UnitStatus>();

    void OnEnable()
    {
        GraveList = GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().GraveList;

        if (GraveList.Count == 0)   // nobody dead
        {
            transform.parent.GetChild(1).gameObject.SetActive(true);
        }
        else if (GraveList.Count != transform.childCount) // 시체랑 묘지 수가 다를때
        {
            transform.parent.GetChild(1).gameObject.SetActive(false);

            int tempIndex = transform.childCount;
            for (int i = 0; i < GraveList.Count - tempIndex; ++i)
            {
                CreateMemory(tempIndex + i);
            }

            MemoriesPositioning();
        }
        else // 시체 수 == 묘지 수
        {
            MemoriesPositioning();
        }
    }


    void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") < 0 && transform.localPosition.x < (transform.childCount - 1) * 350 * -1)
        {
            return;
        }
        else if (Input.GetAxis("Mouse ScrollWheel") > 0 && transform.localPosition.x >= 0)
        {
            return;
        }

        transform.localPosition = new Vector3(transform.localPosition.x + Input.GetAxis("Mouse ScrollWheel") * 350, transform.localPosition.y);

        if (transform.localPosition.x < (transform.childCount - 1) * 350 * -1)
            transform.localPosition = new Vector3((transform.childCount - 1) * 350 * -1, 0);
        else if (transform.localPosition.x > 0)
            transform.localPosition = Vector3.zero;
    }


    void CreateMemory(int iIndex)
    {
        GameObject tempMemory = Instantiate(Resources.Load("Prefaps/UI/StaticUI/Stat/Memories") as GameObject) as GameObject;
        tempMemory.transform.parent = transform;

        tempMemory.transform.FindChild("Portrait").GetComponent<UISprite>().spriteName = GraveList[iIndex].m_unitData.strFaceName;
        tempMemory.transform.FindChild("Name").GetComponent<UILabel>().text = string.Format("{0} {1}", GraveList[iIndex].m_unitData.strFirstName, GraveList[iIndex].m_unitData.strLastName);
        tempMemory.transform.FindChild("Birth").GetComponent<UILabel>().text = string.Format("{0} {1}, 19{2}", RandomingMonth(), Random.Range(0, 31), Random.Range(55, 75));
        tempMemory.transform.FindChild("Dead").GetComponent<UILabel>().text = string.Format("{0} {1}, {2}", GraveList[iIndex].m_unitData.strDeadMonth, GraveList[iIndex].m_unitData.iDeadDay, GraveList[iIndex].m_unitData.iDeadYear);
        tempMemory.transform.FindChild("Diefrom").GetComponent<UILabel>().text = GraveList[iIndex].m_unitData.strDeadReason;
    }


    void MemoriesPositioning()
    {
        for (int i = 0; i < transform.childCount; ++i)
            {
                transform.GetChild(transform.childCount - 1 - i).localPosition = new Vector3(i*350,0);
                transform.GetChild(transform.childCount - 1 - i).localScale = Vector3.one;
            }
    }


    string RandomingMonth()
    {
        string tempMonth;

        switch (Random.Range(1, 13))
        {
            case 1:
                tempMonth = "Jan";
                break;
            case 2:
                tempMonth = "Feb";
                break;
            case 3:
                tempMonth = "Mar";
                break;
            case 4:
                tempMonth = "Apr";
                break;
            case 5:
                tempMonth = "May";
                break;
            case 6:
                tempMonth = "Jun";
                break;
            case 7:
                tempMonth = "Jul";
                break;
            case 8:
                tempMonth = "Aug";
                break;
            case 9:
                tempMonth = "Sep";
                break;
            case 10:
                tempMonth = "Oct";
                break;
            case 11:
                tempMonth = "Nov";
                break;
            case 12:
                tempMonth = "Dec";
                break;

            default:
                tempMonth = "FUCK";
                break;
        }

        return tempMonth;
    }
}
